﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace TBG.TBG_F
{
    class TileWorldManager
    {
        #region attributes
        Texture2D _SpriteSheet;
        //SpriteFont _Font; //for testing purposes
        //all squares of the map only squares without textures
        List<Tile> _Tiles = new List<Tile>();
        //world parameters
        int _squareAcross = 20;
        int _squareDown = 9;
        #endregion

        #region Properties
        public List<Tile> TILES { get { return _Tiles; } set { _Tiles = value; } }
        #endregion

        #region Constructor
        public TileWorldManager(ContentManager content)
        {
            for (int y = 0; y < _squareDown; y++)
            {
                for (int x = 0; x < _squareAcross; x++)
                {
                    Tile tile = new Tile(x, y);
                    _Tiles.Add(tile);
                }
            }
            LoadContent(content);
        }
        #endregion

        #region LoadContent, Update, Draw
        public void LoadContent(ContentManager content)
        {
            _SpriteSheet = content.Load<Texture2D>("tileSet");
            //_Font = content.Load<SpriteFont>("Pericles8");
        }

        public void Update(GameTime gameTime)
        {
            //mouseClickDetector();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Tile tile in _Tiles)
            {
                //first draw method makes sure to draw map tile al the time
                spriteBatch.Draw(_SpriteSheet, tile.getBoundingRect(), tile.getSourceRectBackground(), Color.White);  
                spriteBatch.Draw(_SpriteSheet, tile.getBoundingRect(), tile.getSourceRect(), Color.White);

                //For testing to know each sqaure position
                //spriteBatch.DrawString(_Font, tile._Position.ToString(), new Vector2(tile.getBoundingRect().X,tile.getBoundingRect().Y), Color.White);
            }
        }
        #endregion

        #region Tiles selection
        public void selectSqaures(Point middle)
        {
            Rectangle PlayerRect = new Rectangle((int)getLocationByPoint(middle).X, (int)getLocationByPoint(middle).Y, 1, 1);

            foreach (Tile tile in _Tiles)
            {
                if (PlayerRect.Intersects(tile.getBoundingRect()))
                {
                    selectTilesByPoint(tile.selectedPoint()); //the rest of the checking is made inside method
                }
            }
        }

        private void selectTilesByPoint(List<Point> points)
        {
            foreach (Tile tile in _Tiles)
            {
                if (points.Contains(tile._Position))
                {
                    tile._State = TileStates.Blocked;
                }
                else
                {
                    tile._State = TileStates.Passable;
                }
            }
        }

        public Vector2 getLocationByPoint(Point point)
        {
            Vector2 Location = new Vector2(0, 0);
            Location = new Vector2(
                (1 + TILES[0]._Width) * point.X, 
                (1 + TILES[0]._Height) * point.Y);
            return Location;
        }
        #endregion
    }
}
